import { RetailFundType } from '../common/retailDefinition.js';
import { FundAccount } from '../funds/fundAccount.js';
import { WareHouse } from '../inventory/warehouse.js';
import { Store } from '../store/store.js';
import { Util } from '../../utils/util.js';
import { RetailFundAccount } from './retailFundAccount.js';

export class StoreRetailSettings {
    constructor({
        id = '',
        store,
        wareHouse = null,
        cashAccount = null,
        wechatAccount = null,
        alipayAccount = null,
        otherAccount = null,
    }) {
        this.id = id;
        this.store = store;
        this.wareHouse = wareHouse;

        this.cashAccount = cashAccount ?? new RetailFundAccount({ fundType: RetailFundType.Cash });
        this.wechatAccount = wechatAccount ?? new RetailFundAccount({ fundType: RetailFundType.Wechat });
        this.alipayAccount = alipayAccount ?? new RetailFundAccount({ fundType: RetailFundType.AliPay });
        this.otherAccount = otherAccount ?? new RetailFundAccount({ fundType: RetailFundType.OtherPay });
    }

    static fromJson(json) {
        const store = new Store({
            id: Util.jsonToString(json.storeId),
            name: Util.jsonToString(json.storeName),
            employees: [],
        });

        const wareHouse = json.warehouseId
            ? new WareHouse({
                id: Util.jsonToString(json.warehouseId),
                name: Util.jsonToString(json.warehouseName),
            })
            : null;

        const instance = new StoreRetailSettings({ id: Util.jsonToString(json.id), store, wareHouse });

        if (json.cashAccountId && json.cashAccountName) {
            instance.cashAccount.fundAccount = new FundAccount({
                id: Util.jsonToString(json.cashAccountId),
                name: Util.jsonToString(json.cashAccountName),
                balance: 0,
                income: 0,
                expenses: 0,
            });
        }

        if (json.wechatPayAccountId && json.wechatPayAccountName) {
            instance.wechatAccount.fundAccount = new FundAccount({
                id: Util.jsonToString(json.wechatPayAccountId),
                name: Util.jsonToString(json.wechatPayAccountName),
                balance: 0,
                income: 0,
                expenses: 0,
            });
        }

        if (json.aliPayAccountId && json.aliPayAccountName) {
            instance.alipayAccount.fundAccount = new FundAccount({
                id: Util.jsonToString(json.aliPayAccountId),
                name: Util.jsonToString(json.aliPayAccountName),
                balance: 0,
                income: 0,
                expenses: 0,
            });
        }

        if (json.otherPayAccountId && json.otherPayAccountName) {
            instance.otherAccount.fundAccount = new FundAccount({
                id: Util.jsonToString(json.otherPayAccountId),
                name: Util.jsonToString(json.otherPayAccountName),
                balance: 0,
                income: 0,
                expenses: 0,
            });
        }

        return instance;
    }

    validateSettingData() {
        const msg = this._validateSettingData();
        return msg ? `${this.store.name} 零售参数未设置完整，请检查` : null;
    }

    _validateSettingData() {
        if (!this.wareHouse || !this.wareHouse.id) return '请设置出库仓库';
        if (!this.cashAccount.fundAccount?.id) return '请设置现金账户';
        if (!this.wechatAccount.fundAccount?.id) return '请设置微信账户';
        if (!this.alipayAccount.fundAccount?.id) return '请设置支付宝账户';
        if (!this.otherAccount.fundAccount?.id) return '请设置其他账户';
        return null;
    }

    getRetailFundAccountByType(type) {
        switch (type) {
            case RetailFundType.Cash:
                return this.cashAccount;
            case RetailFundType.Wechat:
                return this.wechatAccount;
            case RetailFundType.AliPay:
                return this.alipayAccount;
            case RetailFundType.OtherPay:
                return this.otherAccount;
            default:
                return null;
        }
    }

    setRetailFundAccount(type, fundAccount) {
        switch (type) {
            case RetailFundType.Cash:
                this.cashAccount.fundAccount = fundAccount;
                break;
            case RetailFundType.Wechat:
                this.wechatAccount.fundAccount = fundAccount;
                break;
            case RetailFundType.AliPay:
                this.alipayAccount.fundAccount = fundAccount;
                break;
            case RetailFundType.OtherPay:
                this.otherAccount.fundAccount = fundAccount;
                break;
        }
    }

    toJson() {
        return {
            id: this.id,
            storeId: this.store.id,
            warehouseId: this.wareHouse?.id,
            cashAccountId: this.cashAccount.fundAccount?.id,
            wechatPayAccountId: this.wechatAccount.fundAccount?.id,
            aliPayAccountId: this.alipayAccount.fundAccount?.id,
            otherPayAccountId: this.otherAccount.fundAccount?.id,
        };
    }
}
